﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MpSoft.Library.DTO;
using MpSoft.Library.DAO.Categorys;
using MpSoft.Basic.Utils;
using MpSoft.Basic.Configuration;

namespace MpSoft.Library.BUSS.Categories
{
    public class MenuBUSS
    {
        public const string KEY_MENU_BYID = "key.menu.by-{0}";

        #region Change Menu

        public static void RemoveCacheMenu()
        {
            MpCache.RemoveByPattern(KEY_MENU_BYID);
        }

        public static int Menu_Add(Menu obj)
        {
            RemoveCacheMenu();
            return MenuDAO.Menu_Add(obj);
        }

        public static int Menu_Update(Menu obj)
        {
            RemoveCacheMenu();
            return MenuDAO.Menu_Update(obj);
        }

        public static int Menu_Delete(string MenuCode)
        {
            RemoveCacheMenu();
            return MenuDAO.Menu_Delete(MenuCode);
        }

        public static List<Menu> Menu_List()
        {
            return MenuDAO.Menu_List();
        }

        #endregion

        #region HTML MENU
        ///<summary>
        ///Menu By ParentID And MenuID
        ///</summary>
        public static string GetByMenuID(int PrentID, string MenuID)
        {
            string key = string.Format(KEY_MENU_BYID, MenuID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return obj.ToString();
            var obj_mn = GetByMenu_Ul(PrentID, MenuID);
            if (!string.IsNullOrEmpty(obj_mn))
                MpCache.Max(key, obj_mn);
            return obj_mn;
        }
        ///<summary>
        ///Menu By MenuID
        ///</summary>
        private static string GetByMenu_Ul(int parent, string menuid)
        {
            var listMenu = CategoryBUSS.Category_ByParentID(parent, string.Empty, menuid, 1);
            if (listMenu != null)
            {
                StringBuilder sb = new StringBuilder(342);
                string url = string.Empty;
                sb.Append("<ul>");
                if (menuid.Equals("menu-top"))
                    sb.Append("<li class='root'><a href='/'><img src='/Images/icon-home.png'/>Trang chủ</a></li>");
                foreach (var obj in listMenu)
                {
                    url = SEOHelper.GetCategoryUrl(obj.SeName);
                    sb.Append("<li>");
                    sb.Append("<a target='" + obj.Target + "' href='" + (string.IsNullOrEmpty(obj.Link) ? url : obj.Link) + "'>" + obj.CategoryName + "</a>");
                    var subMenu = CategoryBUSS.Category_ByParentID(obj.CategoryID, string.Empty, menuid, 1);
                    sb.Append(GetByMenu_Ul2(obj.CategoryID, menuid, subMenu));
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
                return sb.ToString();
            }
            return "";
        }
        ///<summary>
        ///Sub Menu
        ///</summary>
        private static string GetByMenu_Ul2(int parent, string menuid, List<Category> listCate)
        {
            if (listCate != null)
            {
                StringBuilder sb = new StringBuilder(342);
                string url = string.Empty;
                sb.Append("<ul>");
                foreach (var obj in listCate)
                {
                    url = SEOHelper.GetCategoryUrl(obj.SeName);
                    sb.Append("<li>");
                    sb.Append("<a target='" + obj.Target + "' href='" + url + "'>" + obj.CategoryName + "</a>");
                    var subMenu = CategoryBUSS.Category_ByParentID(obj.CategoryID, string.Empty, menuid, 1);
                    sb.Append(GetByMenu_Ul2(obj.CategoryID, menuid, subMenu));
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
                return sb.ToString();
            }
            return "";
        }
        ///<summary>
        ///Menu Footer
        ///</summary>
        #region Footer

        public static string GetByFooter(int PrentID, string MenuID)
        {
            string key = string.Format(KEY_MENU_BYID, MenuID);
            object obj = MpCache.Get(key);
            if (obj != null)
                return obj.ToString();
            var obj_mn = GetByMenuFooter_Ul(PrentID, MenuID);
            if (!string.IsNullOrEmpty(obj_mn))
                MpCache.Max(key, obj_mn);
            return obj_mn;
        }
        ///<summary>
        ///Sub Menu
        ///</summary>
        private static string GetByMenuFooter_Ul(int parent, string menuid)
        {
            var listMenu = CategoryBUSS.Category_ByParentID(parent, string.Empty, menuid, 1);
            if (listMenu != null)
            {
                StringBuilder sb = new StringBuilder(342);
                string url = string.Empty;
                sb.Append("<ul>");
                foreach (var obj in listMenu)
                {
                    url = SEOHelper.GetCategoryUrl(obj.SeName);
                    sb.Append("<li>");
                    sb.Append("<h3><a target='" + obj.Target + "' href='" + (string.IsNullOrEmpty(obj.Link) ? url : obj.Link) + "'>" + obj.CategoryName + "</a></h3>");
                    var subMenu = CategoryBUSS.Category_ByParentID(obj.CategoryID, string.Empty, menuid, 1);
                    sb.Append(GetByMenuFooter_a(obj.CategoryID, menuid, subMenu));
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
                return sb.ToString();
            }
            return "";
        }
        
        private static string GetByMenuFooter_a(int parent, string menuid, List<Category> listCate)
        {
            if (listCate != null)
            {
                StringBuilder sb = new StringBuilder(342);
                string url = string.Empty;
                foreach (var obj in listCate)
                {
                    url = SEOHelper.GetCategoryUrl(obj.SeName);
                    sb.Append("<a target='" + obj.Target + "' href='" + (string.IsNullOrEmpty(obj.Link) ? url : obj.Link) + "'>" + obj.CategoryName + "</a>");
                    var subMenu = CategoryBUSS.Category_ByParentID(obj.CategoryID, string.Empty, menuid, 1);
                    sb.Append(GetByMenuFooter_a(obj.CategoryID, menuid, subMenu));
   
                }
                return sb.ToString();
            }
            return "";
        }
        
        #endregion

        #endregion
    }
}
